package com.dianxin.farm.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dianxin.farm.entity.GoodsInfo;
import com.dianxin.farm.entity.GoodsInfoImage;
import com.dianxin.farm.filter.AuthorCheck;
import com.dianxin.farm.request.common.IdRequest;
import com.dianxin.farm.request.goods.EditGoodsRequest;
import com.dianxin.farm.request.goods.QueryGoodsRequest;
import com.dianxin.farm.request.goods.SaveGoodsRequest;
import com.dianxin.farm.response.goods.GoodsPageResponse;
import com.dianxin.farm.service.GoodsService;
import com.dianxin.farm.service.IGoodsInfoImageService;
import com.dianxin.farm.service.IGoodsInfoService;
import com.dianxin.farm.util.util.StringUtil;
import com.dianxin.farm.util.web.ApiResponse;
import com.dianxin.farm.util.web.ApiResponseUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品基础信息表 前端控制器
 * </p>
 *
 * @author xukai
 * @since 2020-09-14
 */
@RestController
@RequestMapping(value = "/goodsInfo", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
@Api(description = "服务商品控制器")
public class GoodsInfoController {
    @Resource
    private GoodsService goodsService;
    @Resource
    private IGoodsInfoService goodsInfoService;
    @Resource
    private IGoodsInfoImageService goodsImageService;

    @ApiOperation("发布商品")
    @RequestMapping(value = "/publishGoods")
    @ResponseBody
    @AuthorCheck
    public ApiResponse publishGoods(@Valid @RequestBody SaveGoodsRequest request) {
        goodsService.saveGoods(request);
        return ApiResponseUtils.success();
    }


    @ApiOperation("编辑商品，包括图片也可以替换。上传全量地址数组，删除图片，调用单独的删除接口")
    @RequestMapping(value = "/editGoods")
    @ResponseBody
    @AuthorCheck
    public ApiResponse editGoods(@Valid @RequestBody EditGoodsRequest request) {
        goodsService.editGoods(request);
        return ApiResponseUtils.success();
    }

    @ApiOperation("下架商品")
    @RequestMapping(value = "/unShelfGoods")
    @ResponseBody
    @AuthorCheck
    public ApiResponse unShelfGoods(@Valid @RequestBody IdRequest request) {
        Integer goodsId = request.getId();
        GoodsInfo goodsInfo = goodsInfoService.getById(goodsId);
        if (goodsInfo != null) {
            goodsInfo.setShelfStatus(0);
            goodsInfoService.updateById(goodsInfo);
        }

        return ApiResponseUtils.success();
    }

    @ApiOperation("上架商品")
    @RequestMapping(value = "/shelfGoods")
    @ResponseBody
    @AuthorCheck
    public ApiResponse shelfGoods(@Valid @RequestBody IdRequest request) {
        Integer goodsId = request.getId();
        GoodsInfo goodsInfo = goodsInfoService.getById(goodsId);
        if (goodsInfo != null) {
            goodsInfo.setShelfStatus(1);
            goodsInfoService.updateById(goodsInfo);
        }
        return ApiResponseUtils.success();
    }


    @ApiOperation("删除某个商品")
    @RequestMapping(value = "/killGoods")
    @ResponseBody
    @AuthorCheck
    @Transactional
    public ApiResponse killGoods(@Valid @RequestBody IdRequest request) {
        Integer goodsId = request.getId();
        goodsInfoService.removeById(goodsId);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("goods_info_id",goodsId);
        goodsImageService.remove(wrapper);
        //TODO..  删除minio文件库中的图片
        return ApiResponseUtils.success();
    }

    @ApiOperation("删除商品的图片，单张")
    @RequestMapping(value = "/killGoodsImage")
    @ResponseBody
    @AuthorCheck
    @Transactional
    public ApiResponse killGoodsImage(@Valid @RequestBody IdRequest request) {
        Integer goodsImageId = request.getId();
        goodsImageService.removeById(goodsImageId);
        //TODO..  删除minio文件库中的图片
        return ApiResponseUtils.success();
    }



    @ApiOperation("商品列表分页")
    @RequestMapping(value = "/goodsPages")
    @ResponseBody
    @AuthorCheck
    @Transactional
    public ApiResponse<PageInfo<GoodsPageResponse>> goodsPages(@Valid @RequestBody QueryGoodsRequest request) {
        String goodsName= request.getGoodsName();
        Long goodsCode=request.getGoodsCode();
        Integer status=request.getShelfStatus();
        PageHelper.startPage(request.getPage(),request.getSize());
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like(StringUtil.isNotBlank(goodsName),"goods_name",goodsName);
        wrapper.eq(goodsCode!=null,"goods_code",goodsCode);
        wrapper.eq(status!=null,"shelf_status",status);
        wrapper.orderByDesc("top_status");
        wrapper.orderByDesc("id");
        List<GoodsInfo> goodsInfos=goodsInfoService.list(wrapper);
        PageInfo goodsPages=new PageInfo<>(goodsInfos);

        List<GoodsPageResponse> responses=new ArrayList<>();
        if (goodsInfos!=null && goodsInfos.size()>0){
            for (GoodsInfo goodsInfo:goodsInfos){
                GoodsPageResponse response=new GoodsPageResponse();
                response.setGoodsInfo(goodsInfo);
                Integer goodsId=goodsInfo.getId();
                QueryWrapper imgeWr=new QueryWrapper();
                imgeWr.eq("goods_info_id",goodsId);
                List<GoodsInfoImage> images=goodsImageService.list(imgeWr);
                response.setGoodsImages(images);
                responses.add(response);
            }
        }

        PageInfo pageInfo=new PageInfo<>(responses);
        pageInfo.setTotal(goodsPages.getTotal());
        return ApiResponseUtils.success(pageInfo);
    }



    @ApiOperation("置顶商品")
    @RequestMapping(value = "/top")
    @ResponseBody
    @AuthorCheck
    public ApiResponse top(@Valid @RequestBody IdRequest request) {
        Integer goodsInfoId=request.getId();
        GoodsInfo goodsInfo=goodsInfoService.getById(goodsInfoId);
        if (goodsInfo!=null){
            goodsInfo.setTopStatus(true);
            goodsInfoService.updateById(goodsInfo);
        }
        return ApiResponseUtils.success();
    }

    @ApiOperation("取消置顶商品")
    @RequestMapping(value = "/unTop")
    @ResponseBody
    @AuthorCheck
    public ApiResponse unTop(@Valid @RequestBody IdRequest request) {
        Integer goodsInfoId=request.getId();
        GoodsInfo goodsInfo=goodsInfoService.getById(goodsInfoId);
        if (goodsInfo!=null){
            goodsInfo.setTopStatus(false);
            goodsInfoService.updateById(goodsInfo);
        }
        return ApiResponseUtils.success();
    }





}
